@extends('layouts.template')

@section('title', '管理后台')

@section('content')

    <script>
        $(function(){

            var form = new Vue({
                el:'#contentpanel',
                data:{
                },
                computed:{
                },
                mounted:function(){
                },
                watch: {
                },
                methods:{
                },
                components:{
                }
            });

            $('#edit').on('show.bs.modal', function (event) {
                var button = $(event.relatedTarget);
                var modal = $(this);
                modal.find('[name=manager_id]').val(button.data('id'));
                modal.find('[name=name]').val(button.data('name'));
                modal.find('[name=email]').val(button.data('email'));
            });

            $('#editPass').on('show.bs.modal', function (event) {
                var button = $(event.relatedTarget);
                var modal = $(this);
                modal.find('[name=manager_id]').val(button.data('id'));
            });

            var validator = $("#add-form").validate({
                rules: {
                    name:{
                        required:true,
                        minlength:4,
                    },
                    email:{
                        required:true,
                        email:true,
                    },
                    password:{
                        required:true,
                        minlength:4,
                    },
                },
                messages: {
                    name:{
                        required:'请输入用户名',
                    },
                    email:{
                        required:'请输入邮箱地址',
                    },
                    password:{
                        required:'请输入密码',
                    },
                },
                submitHandler: function(form) {
                    form.submit();
                },
            });

            var validator = $("#edit-form").validate({
                rules: {
                    manager_id:{
                        required:true,
                    },
                    name:{
                        required:true,
                        minlength:4,
                    },
                    email:{
                        required:true,
                        email:true,
                    },
                },
                messages: {
                    manager_id:{
                        required:'缺少管理员id',
                    },
                    name:{
                        required:'请输入用户名',
                    },
                    email:{
                        required:'请输入邮箱地址',
                    },
                },
                submitHandler: function(form) {
                    form.submit();
                },
            });

            var validator = $("#editPass-form").validate({
                rules: {
                    manager_id:{
                        required:true,
                    },
                    password:{
                        required:true,
                        minlength:4,
                    },
                    password_confirmation:{
                        required:true,
                        equalTo:"#editPass-form [name=password]"
                    },
                },
                messages: {
                    manager_id:{
                        required:'缺少管理员id',
                    },
                    password:{
                        required:'请输入新密码',
                    },
                    password_confirmation:{
                        required:'请重复新密码',
                        equalTo:"两次输入不一致"
                    },
                },
                submitHandler: function(form) {
                    form.submit();
                },
            });
        });

    </script>

    <div class="panel-body">
        <div class="panel panel-default">
            <div class="panel-body">
                <form action="" method="GET" class="form-inline">
                    <div>
                        {!! $filter !!}
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="contentpanel" id="contentpanel">

        <div class="form-group">
            <label>
                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#add">+ 新建管理员</button>
            </label>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="table-responsive">
                    <table class="table mb30">
                        <thead>
                        <tr style="white-space:nowrap">
                            <th><input type="checkbox" onclick="checkall(this)">选择</th>
                            <th>用户名</th>
                            <th>邮箱地址</th>
                            <th>创建时间</th>
                            <th>最后登录时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($list as $row):?>
                            <tr data-id="{{$row->id}}">
                                <td><input type="checkbox" value="{{$row->id}}" name="user[]"></td>
                                <td>{{$row->name}}</td>
                                <td>{{$row->email}}</td>
                                <td>{{$row->created_at}}</td>
                                <td>{{$row->updated_at}}</td>
                                <td>
                                    <button type="button" class="btn btn-white" data-toggle="modal" data-target="#edit" data-id="{{$row->id}}" data-name="{{$row->name}}" data-email="{{$row->email}}">编辑</button>
                                    <button type="button" class="btn btn-white" data-toggle="modal" data-target="#editPass" data-id="{{$row->id}}">修改密码</button>
                                    {{--<a href="{{url("admin/enable/{$row->id}")}}" onclick="return confirm('启用该用户')" class="btn btn-success btn-metro">启用</a>--}}
                                    <a href="{{url("admin/delete/{$row->id}")}}" onclick="return confirm('请再次确认是否删除该用户')" class="btn btn-danger btn-metro">删除</a>
                                </td>
                            </tr>
                        <?php endforeach;?>
                        </tbody>
                    </table>
                </div>
                <!-- table-responsive -->
                {{ @!$list->isEmpty() ? $list->appends(request()->all())->links() : '' }}
            </div>
            <!-- col-md-12 -->
        </div>
    </div><!-- contentpanel -->

    <div class="modal fade" id="add" tabindex="-1" role="dialog" data-keyboard="false">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">新建管理员</h4>
                </div>
                <form id="add-form" method="post" action="{{url("admin/add")}}">
                    {{csrf_field()}}
                    <div class="modal-body">
                        <div class="form-group">
                            <input type="text" class="form-control" name="name" placeholder="用户名">
                        </div>
                        <div class="form-group">
                            <input type="email" class="form-control" name="email" placeholder="邮箱地址">
                        </div>
                        <div class="form-group">
                            <input type="password" class="form-control" name="password" placeholder="密码">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="submit" class="btn btn-primary">创建</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="modal fade" id="edit" tabindex="-1" role="dialog" data-keyboard="false">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">编辑</h4>
                </div>
                <form id="edit-form" method="post" action="{{url("admin/edit")}}">
                    {{csrf_field()}}
                    <input type="hidden" name="manager_id">
                    <div class="modal-body">
                        <div class="form-group">
                            <input type="text" class="form-control" name="name" placeholder="用户名">
                        </div>
                        <div class="form-group">
                            <input type="email" class="form-control" name="email" placeholder="邮箱地址">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="submit" class="btn btn-primary">修改</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="modal fade" id="editPass" tabindex="-1" role="dialog" data-keyboard="false">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">修改密码</h4>
                </div>
                <form id="editPass-form" method="post" action="{{url("admin/editpass")}}">
                    {{csrf_field()}}
                    <input type="hidden" name="manager_id">
                    <div class="modal-body">
                            <div class="form-group">
                                <input type="password" class="form-control" name="password" placeholder="新密码">
                            </div>
                            <div class="form-group">
                                <input type="password" class="form-control" name="password_confirmation" placeholder="重复密码">
                            </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="submit" class="btn btn-primary">修改</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

@endsection

